package com.xmqian.app.project.ui;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import androidx.annotation.Nullable;

import com.coszero.common.config.Constant;
import com.coszero.utils.utils.LogX;
import com.coszero.utils.utils.SharedPreUtils;
import com.xmqian.app.project.common.base.BaseMyFragment;
import com.xmqian.app.project.ui.user.activity.LoginActivity;
import com.xmqian.kotlinApp.databinding.FragmentGuideBinding;


/**
 * Desc： 引导界面
 * <p>
 * Author： xmqian
 * Email:xmqian93@163.com
 * Date: 2019/6/10 16:16
 *
 * @link(com.xmqian.app.project.GuideActivity)
 */
public class GuideFragment extends BaseMyFragment {
    ImageView mIvGuide;

    private int[] resoursArray;
    public static final String RESOURS_ARRAY = "resoursArray";
    public static final String POS = "pos";
    private int index;
    private boolean isFrist = true;
    private FragmentGuideBinding binding;

    public static GuideFragment newInstance(int[] resoursArray, int pos) {
        Bundle args = new Bundle();
        GuideFragment fragment = new GuideFragment();
        args.putIntArray(RESOURS_ARRAY, resoursArray);
        args.putInt(POS, pos);
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void initView() {
        mIvGuide = binding.ivGuide;
        resoursArray = getArguments().getIntArray(RESOURS_ARRAY);
        index = getArguments().getInt(POS, 0);
        mIvGuide.setImageResource(resoursArray[index]);
        if (index == 0) {
            mIvGuide.setImageResource(resoursArray[index]);
        }
        if (resoursArray.length - 1 == index) {
            mIvGuide.setOnClickListener(v -> {
                SharedPreUtils.saveInteger(mContext, Constant.JUMP_TYPE, Constant.GUIDE_FINISH);
                LoginActivity.startNewTask(getActivity());
                getActivity().finish();
            });
        }
        isFrist = false;
    }

    @Override
    protected void initData() {

    }

    @Override
    public void lazyLoadData() {
        if (resoursArray != null && !isFrist) {
            mIvGuide.setBackgroundResource(resoursArray[index]);
            LogX.i("### 引导加载第" + (index + 1) + "页");
        }
    }

    @Override
    public View getLayoutView(LayoutInflater inflater, @Nullable ViewGroup container, boolean attachToParent) {
        binding = FragmentGuideBinding.inflate(inflater, container, attachToParent);
        return binding.getRoot();
    }
}
